/**
 * 作者: wgx
 * 日期: 2025-03-14 17:48:39
 * 备注: 物资争夺，的玩家队伍配置表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSnatchTeam;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSnatchTeamModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.team_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 玩家id
    protected int teamId; // 队伍id (1,2,3,4,5...20)
    protected String partner = ""; // 上阵的干员id列表
    protected int isInspire; // 是否鼓舞20次
    protected long enemyId; // 匹配到对手id
    protected int koNum; // 打败对手N名干员
    protected int failNum; // 我方被打败N名干员
    protected int fightNum; // 战斗几次
    protected long transferItemNum; // 获得中转站的扩建工具的数量
    protected long snatchMoneyNum; // 获得贸易币的数量
    protected int addScore; // 获得的物资数量
    protected int subScore; // 对方被扣的物资数量
    protected int inspireNum; // 实际鼓舞次数

    public void reset() {
    }

    protected String getKey() {
        return "UserSnatchTeam." + userId + "." + teamId;
    }

    protected String getListKey() {
        return "UserSnatchTeam." + userId;
    }

    protected String getListItemKey() {
        return teamId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_snatch_team"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 玩家id
        obj.put("teamId", teamId); // 队伍id (1,2,3,4,5...20)
        obj.put("partner", partner); // 上阵的干员id列表
        obj.put("isInspire", isInspire); // 是否鼓舞20次
        obj.put("enemyId", enemyId); // 匹配到对手id
        obj.put("koNum", koNum); // 打败对手N名干员
        obj.put("failNum", failNum); // 我方被打败N名干员
        obj.put("fightNum", fightNum); // 战斗几次
        obj.put("transferItemNum", transferItemNum); // 获得中转站的扩建工具的数量
        obj.put("snatchMoneyNum", snatchMoneyNum); // 获得贸易币的数量
        obj.put("addScore", addScore); // 获得的物资数量
        obj.put("subScore", subScore); // 对方被扣的物资数量
        obj.put("inspireNum", inspireNum); // 实际鼓舞次数
        return obj;
    }

    public PBUserSnatchTeam.Builder takeInitBuilder() {
        PBUserSnatchTeam.Builder sb = PBUserSnatchTeam.newBuilder();
        if (teamId != 0) {
            sb.setTeamId(teamId);
        }
        if (this.partner != null && this.partner.length() > 0) {
            JsonArray list = new JsonArray(this.partner);
            for(int i=0; i<list.size(); i++) {
                sb.addPartner(list.getInteger(i));
            }
        }
        if (isInspire != 0) {
            sb.setIsInspire(isInspire);
        }
        if (enemyId != 0) {
            sb.setEnemyId(enemyId);
        }
        if (koNum != 0) {
            sb.setKoNum(koNum);
        }
        if (fightNum != 0) {
            sb.setFightNum(fightNum);
        }
        if (transferItemNum != 0) {
            sb.setTransferItemNum(transferItemNum);
        }
        if (snatchMoneyNum != 0) {
            sb.setSnatchMoneyNum(snatchMoneyNum);
        }
        if (addScore != 0) {
            sb.setAddScore(addScore);
        }
        if (subScore != 0) {
            sb.setSubScore(subScore);
        }
        return sb;
    }

    public PBUserSnatchTeam.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSnatchTeam.Builder sb = PBUserSnatchTeam.newBuilder();
        if (isFieldUpdate("teamId")) {
            sb.setTeamId(teamId);
        }
        if (isFieldUpdate("partner")) {
            if (this.partner != null && this.partner.length() > 0) {
                JsonArray list = new JsonArray(this.partner);
                for(int i=0; i<list.size(); i++) {
                    sb.addPartner(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearPartner();
                }
            } else {
                sb.clearPartner();
            }
        }
        if (isFieldUpdate("isInspire")) {
            sb.setIsInspire(isInspire);
        }
        if (isFieldUpdate("enemyId")) {
            sb.setEnemyId(enemyId);
        }
        if (isFieldUpdate("koNum")) {
            sb.setKoNum(koNum);
        }
        if (isFieldUpdate("fightNum")) {
            sb.setFightNum(fightNum);
        }
        if (isFieldUpdate("transferItemNum")) {
            sb.setTransferItemNum(transferItemNum);
        }
        if (isFieldUpdate("snatchMoneyNum")) {
            sb.setSnatchMoneyNum(snatchMoneyNum);
        }
        if (isFieldUpdate("addScore")) {
            sb.setAddScore(addScore);
        }
        if (isFieldUpdate("subScore")) {
            sb.setSubScore(subScore);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getTeamId() {
        return teamId;
    }

    public void putTeamId(int teamId) {
        if (this.teamId != teamId) {
            this.teamId = teamId;
            updateRedisHashField(getKey(), "teamId", this.teamId);
        }
    }

    public void addTeamId(int value) {
        this.teamId += value;
        updateRedisHashField(getKey(), "teamId", this.teamId);
    }

    public JsonArray getPartnerArray() {
        if (this.partner != null && this.partner.length() > 0) {
            return new JsonArray(this.partner);
        } else {
            return new JsonArray();
        }
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public int getIsInspire() {
        return isInspire;
    }

    public void putIsInspire(int isInspire) {
        if (this.isInspire != isInspire) {
            this.isInspire = isInspire;
            updateRedisHashField(getKey(), "isInspire", this.isInspire);
        }
    }

    public void addIsInspire(int value) {
        this.isInspire += value;
        updateRedisHashField(getKey(), "isInspire", this.isInspire);
    }

    public long getEnemyId() {
        return enemyId;
    }

    public void putEnemyId(long enemyId) {
        if (this.enemyId != enemyId) {
            this.enemyId = enemyId;
            updateRedisHashField(getKey(), "enemyId", this.enemyId);
        }
    }

    public void addEnemyId(long value) {
        this.enemyId += value;
        updateRedisHashField(getKey(), "enemyId", this.enemyId);
    }

    public int getKoNum() {
        return koNum;
    }

    public void putKoNum(int koNum) {
        if (this.koNum != koNum) {
            this.koNum = koNum;
            updateRedisHashField(getKey(), "koNum", this.koNum);
        }
    }

    public void addKoNum(int value) {
        this.koNum += value;
        updateRedisHashField(getKey(), "koNum", this.koNum);
    }

    public int getFailNum() {
        return failNum;
    }

    public void putFailNum(int failNum) {
        if (this.failNum != failNum) {
            this.failNum = failNum;
            updateRedisHashField(getKey(), "failNum", this.failNum);
        }
    }

    public void addFailNum(int value) {
        this.failNum += value;
        updateRedisHashField(getKey(), "failNum", this.failNum);
    }

    public int getFightNum() {
        return fightNum;
    }

    public void putFightNum(int fightNum) {
        if (this.fightNum != fightNum) {
            this.fightNum = fightNum;
            updateRedisHashField(getKey(), "fightNum", this.fightNum);
        }
    }

    public void addFightNum(int value) {
        this.fightNum += value;
        updateRedisHashField(getKey(), "fightNum", this.fightNum);
    }

    public long getTransferItemNum() {
        return transferItemNum;
    }

    public void putTransferItemNum(long transferItemNum) {
        if (this.transferItemNum != transferItemNum) {
            this.transferItemNum = transferItemNum;
            updateRedisHashField(getKey(), "transferItemNum", this.transferItemNum);
        }
    }

    public void addTransferItemNum(long value) {
        this.transferItemNum += value;
        updateRedisHashField(getKey(), "transferItemNum", this.transferItemNum);
    }

    public long getSnatchMoneyNum() {
        return snatchMoneyNum;
    }

    public void putSnatchMoneyNum(long snatchMoneyNum) {
        if (this.snatchMoneyNum != snatchMoneyNum) {
            this.snatchMoneyNum = snatchMoneyNum;
            updateRedisHashField(getKey(), "snatchMoneyNum", this.snatchMoneyNum);
        }
    }

    public void addSnatchMoneyNum(long value) {
        this.snatchMoneyNum += value;
        updateRedisHashField(getKey(), "snatchMoneyNum", this.snatchMoneyNum);
    }

    public int getAddScore() {
        return addScore;
    }

    public void putAddScore(int addScore) {
        if (this.addScore != addScore) {
            this.addScore = addScore;
            updateRedisHashField(getKey(), "addScore", this.addScore);
        }
    }

    public void addAddScore(int value) {
        this.addScore += value;
        updateRedisHashField(getKey(), "addScore", this.addScore);
    }

    public int getSubScore() {
        return subScore;
    }

    public void putSubScore(int subScore) {
        if (this.subScore != subScore) {
            this.subScore = subScore;
            updateRedisHashField(getKey(), "subScore", this.subScore);
        }
    }

    public void addSubScore(int value) {
        this.subScore += value;
        updateRedisHashField(getKey(), "subScore", this.subScore);
    }

    public int getInspireNum() {
        return inspireNum;
    }

    public void putInspireNum(int inspireNum) {
        if (this.inspireNum != inspireNum) {
            this.inspireNum = inspireNum;
            updateRedisHashField(getKey(), "inspireNum", this.inspireNum);
        }
    }

    public void addInspireNum(int value) {
        this.inspireNum += value;
        updateRedisHashField(getKey(), "inspireNum", this.inspireNum);
    }

}